package servlet;

import java.io.IOException;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import entity.TbClass;
import service.TbClassService;



/**
 * Servlet implementation class BanjiServlet
 */
@WebServlet("/banji")
public class BanjiServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private TbClassService tbClassService=new TbClassService();
	private ObjectMapper mapper=new ObjectMapper();
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String method = request.getParameter("method");
		if(method==null) {
			list(request,response);
		}else if("update".contentEquals(method)) {
			update(request,response);
		}else if("save".contentEquals(method)) {
			save(request,response);
		}else if("add".contentEquals(method)) {
			add(request,response);
		}else if("delete".contentEquals(method)) {
			delete(request,response);
		}else if("list2".contentEquals(method)) {
			request.getRequestDispatcher("jsp/banji/list2.jsp").forward(request, response);
		}else if("allClass".contentEquals(method)) {
			allClass(request,response);
		}else if("ajaxEdit".contentEquals(method)) {
			aajaxEdit(request,response);
		}else if("ajaxSave".contentEquals(method)){
			ajaxSave(request,response);
		}
	
	}

	private void ajaxSave(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Integer id=Integer.valueOf(request.getParameter("id"));
		String name=request.getParameter("name");
		String manager=request.getParameter("manager");
		tbClassService.save(new TbClass(id, name, manager));
		PrintWriter out=response.getWriter();
		out.print("ok");
		out.close();
	}

	private void aajaxEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Integer id=Integer.valueOf(request.getParameter("id"));
		TbClass modle = tbClassService.findById(id);
		request.setAttribute("modle", modle);
		request.getRequestDispatcher("jsp/banji/ajaxEdit.jsp").forward(request, response);
	}

	private void allClass(HttpServletRequest request, HttpServletResponse response) throws JsonGenerationException, JsonMappingException, IOException {
		List<TbClass> list = tbClassService.findAll();
		String str = mapper.writeValueAsString(list);
		response.setContentType("application/json;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.write(str);
		out.close();
		
	}

	private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String idStr = request.getParameter("id");
		Integer id=Integer.valueOf(idStr);
		tbClassService.delete(id);
		response.sendRedirect("banji");
		
	}

	private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher("jsp/banji/edit.jsp").forward(request, response);
	}

	private void save(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String idStr = request.getParameter("id");
		Integer id=null;
		if(idStr!=null&&!"".contentEquals(idStr)) {
			id=Integer.parseInt(idStr);
		}
		String name = request.getParameter("name");
		String manager = request.getParameter("manager");
		TbClass tbClass=new TbClass(id, name, manager);
		tbClassService.save(tbClass);
		response.sendRedirect("banji");
		
	}

	private void update(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		String idStr = request.getParameter("id");
		Integer id=Integer.parseInt(idStr);
		TbClass model = tbClassService.findById(id);
		request.setAttribute("model",model);
		request.getRequestDispatcher("jsp/banji/edit.jsp").forward(request, response);
		
	}

	private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<TbClass> banji=tbClassService.findAll();
		request.setAttribute("list", banji);
		request.getRequestDispatcher("jsp/banji/list.jsp").forward(request, response);
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
